<template>
  <div class="preview-container" ref="previewContainer" @scroll="onScroll">
    <div class="preview-content" v-html="renderedContent"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { useAppStore } from '../store'
import md from '../utils/markdown'

const store = useAppStore()
const previewContainer = ref<HTMLElement | null>(null)

// 滚动同步标志位，防止循环触发
let isScrollingSyncing = false

// 缓存渲染结果，避免重复渲染相同内容
let cachedContent = ''
let cachedRenderedContent = ''

// 渲染后的内容
const renderedContent = computed(() => {
  const content = store.editorContent
  // 如果内容没有变化，返回缓存的结果
  if (content === cachedContent) {
    return cachedRenderedContent
  }
  
  // 渲染新内容并缓存
  cachedContent = content
  cachedRenderedContent = md.render(content)
  return cachedRenderedContent
})

// 滚动事件处理
const onScroll = (event: Event) => {
  // 如果正在同步滚动，则不触发事件
  if (isScrollingSyncing) return
  
  const target = event.target as HTMLElement
  const scrollInfo = {
    scrollTop: target.scrollTop,
    scrollHeight: target.scrollHeight,
    clientHeight: target.clientHeight
  }
  
  // 发送滚动信息到编辑器组件
  window.dispatchEvent(new CustomEvent('preview-scroll', { detail: scrollInfo }))
}

// 同步滚动处理
const handleEditorScroll = (event: Event) => {
  if (previewContainer.value) {
    const detail = (event as CustomEvent).detail as {
      scrollTop: number
      scrollHeight: number
      clientHeight: number
    }
    
    // 设置同步标志位，防止循环触发
    isScrollingSyncing = true
    
    // 计算预览区域的滚动位置
    const previewScrollTop = 
      (detail.scrollTop / detail.scrollHeight) * previewContainer.value.scrollHeight
    previewContainer.value.scrollTop = previewScrollTop
    
    // 延迟重置标志位，确保滚动事件处理完成
    setTimeout(() => {
      isScrollingSyncing = false
    }, 50)
  }
}

// 组件挂载
onMounted(() => {
  window.addEventListener('editor-scroll', handleEditorScroll)
})

// 组件卸载前
onBeforeUnmount(() => {
  window.removeEventListener('editor-scroll', handleEditorScroll)
})
</script>

<style scoped>
.preview-container {
  height: 100%;
  overflow-y: auto;
  padding: 24px;
  box-sizing: border-box;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(248, 250, 252, 0.95) 100%);
  backdrop-filter: blur(10px);
  color: #2d3748;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  scrollbar-width: thin; /* Firefox */
  scrollbar-color: rgba(102, 126, 234, 0.3) transparent; /* Firefox */
  border-radius: 0 12px 12px 0;
  animation: fadeInUp 0.5s ease-out;
}

/* Webkit浏览器的滚动条样式 */
.preview-container::-webkit-scrollbar {
  width: 8px;
}

.preview-container::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.02);
  border-radius: 4px;
}

.preview-container::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.3) 0%, rgba(118, 75, 162, 0.3) 100%);
  border-radius: 4px;
  transition: all 0.3s ease;
}

.preview-container::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.5) 0%, rgba(118, 75, 162, 0.5) 100%);
}

.preview-content {
  max-width: none;
  line-height: 1.7;
  font-size: 16px;
  font-weight: 400;
  letter-spacing: 0.2px;
}

/* 标题样式 */
.preview-content :deep(h1),
.preview-content :deep(h2),
.preview-content :deep(h3),
.preview-content :deep(h4),
.preview-content :deep(h5),
.preview-content :deep(h6) {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 700;
  line-height: 1.4;
  color: #1a202c;
  position: relative;
}

.preview-content :deep(h1) {
  font-size: 2.25em;
  border-bottom: 3px solid transparent;
  border-image: linear-gradient(90deg, #667eea 0%, #764ba2 100%) 1;
  padding-bottom: 12px;
  margin-bottom: 24px;
}

.preview-content :deep(h2) {
  font-size: 1.875em;
  border-bottom: 2px solid transparent;
  border-image: linear-gradient(90deg, rgba(102, 126, 234, 0.6) 0%, rgba(118, 75, 162, 0.6) 100%) 1;
  padding-bottom: 8px;
  margin-bottom: 20px;
}

.preview-content :deep(h3) {
  font-size: 1.5em;
  color: #2d3748;
}

.preview-content :deep(h4) {
  font-size: 1.25em;
  color: #4a5568;
}

.preview-content :deep(h5) {
  font-size: 1.125em;
  color: #4a5568;
}

.preview-content :deep(h6) {
  font-size: 1em;
  color: #718096;
}

/* 段落样式 */
.preview-content :deep(p) {
  margin-top: 0;
  margin-bottom: 16px;
  line-height: 1.7;
  color: #4a5568;
}

/* 代码块样式 */
.preview-content :deep(pre) {
  padding: 20px;
  overflow: auto;
  font-size: 14px;
  line-height: 1.6;
  background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.05);
  margin: 20px 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  backdrop-filter: blur(5px);
}

.preview-content :deep(pre:hover) {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.preview-content :deep(pre::before) {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px 12px 0 0;
}

/* 行内代码样式 */
.preview-content :deep(code) {
  padding: 4px 8px;
  font-size: 0.9em;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  border-radius: 6px;
  font-family: 'JetBrains Mono', 'Fira Code', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  color: #667eea;
  border: 1px solid rgba(102, 126, 234, 0.2);
  font-weight: 500;
}

/* 引用样式 */
.preview-content :deep(blockquote) {
  padding: 16px 20px;
  color: #4a5568;
  border-left: 4px solid #667eea;
  margin: 20px 0;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  border-radius: 0 8px 8px 0;
  position: relative;
  backdrop-filter: blur(5px);
}

.preview-content :deep(blockquote::before) {
  content: '"';
  position: absolute;
  top: -10px;
  left: 10px;
  font-size: 3em;
  color: rgba(102, 126, 234, 0.3);
  font-family: serif;
}

/* 列表样式 */
.preview-content :deep(ul),
.preview-content :deep(ol) {
  padding-left: 2em;
  margin-top: 0;
  margin-bottom: 16px;
}

.preview-content :deep(li) {
  line-height: 1.7;
  margin-top: 0.5em;
  color: #4a5568;
}

.preview-content :deep(li::marker) {
  color: #667eea;
  font-weight: bold;
}

/* 链接样式 */
.preview-content :deep(a) {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  position: relative;
  transition: all 0.3s ease;
}

.preview-content :deep(a::after) {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  transition: width 0.3s ease;
}

.preview-content :deep(a:hover) {
  color: #5a67d8;
  transform: translateY(-1px);
}

.preview-content :deep(a:hover::after) {
  width: 100%;
}

/* 图片样式 */
.preview-content :deep(img) {
  max-width: 100%;
  box-sizing: border-box;
  background-color: #fff;
  border-radius: 12px;
  margin: 20px 0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.preview-content :deep(img:hover) {
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
  transform: translateY(-4px) scale(1.02);
}

/* 表格样式 */
.preview-content :deep(table) {
  display: block;
  width: 100%;
  overflow: auto;
  border-collapse: collapse;
  margin: 20px 0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
}

.preview-content :deep(th) {
  font-weight: 700;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  text-align: left;
}

.preview-content :deep(td),
.preview-content :deep(th) {
  padding: 12px 16px;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.preview-content :deep(tr:nth-child(even)) {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.02) 0%, rgba(118, 75, 162, 0.02) 100%);
}

.preview-content :deep(tr:hover) {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  transform: scale(1.01);
  transition: all 0.2s ease;
}

/* 分割线样式 */
.preview-content :deep(hr) {
  height: 3px;
  padding: 0;
  margin: 32px 0;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  border: 0;
  border-radius: 2px;
  opacity: 0.6;
}

/* 添加动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 暗色主题 */
.dark .preview-container {
  background: linear-gradient(135deg, rgba(26, 32, 44, 0.95) 0%, rgba(45, 55, 72, 0.95) 100%);
  backdrop-filter: blur(10px);
  color: #e2e8f0;
  scrollbar-color: rgba(76, 81, 191, 0.3) transparent; /* Firefox */
}

/* Webkit浏览器的滚动条样式 - 暗色主题 */
.dark .preview-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.02);
}

.dark .preview-container::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.3) 0%, rgba(85, 60, 154, 0.3) 100%);
}

.dark .preview-container::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.5) 0%, rgba(85, 60, 154, 0.5) 100%);
}

.dark .preview-content :deep(h1),
.dark .preview-content :deep(h2),
.dark .preview-content :deep(h3),
.dark .preview-content :deep(h4),
.dark .preview-content :deep(h5),
.dark .preview-content :deep(h6) {
  color: #f7fafc;
}

.dark .preview-content :deep(h1) {
  border-image: linear-gradient(90deg, #4c51bf 0%, #553c9a 100%) 1;
}

.dark .preview-content :deep(h2) {
  border-image: linear-gradient(90deg, rgba(76, 81, 191, 0.6) 0%, rgba(85, 60, 154, 0.6) 100%) 1;
}

.dark .preview-content :deep(p) {
  color: #cbd5e0;
}

.dark .preview-content :deep(pre) {
  background: linear-gradient(135deg, rgba(45, 55, 72, 0.8) 0%, rgba(26, 32, 44, 0.8) 100%);
  border-color: rgba(255, 255, 255, 0.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.dark .preview-content :deep(pre:hover) {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.dark .preview-content :deep(pre::before) {
  background: linear-gradient(90deg, #4c51bf 0%, #553c9a 100%);
}

.dark .preview-content :deep(code) {
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.2) 0%, rgba(85, 60, 154, 0.2) 100%);
  color: #a78bfa;
  border-color: rgba(76, 81, 191, 0.3);
}

.dark .preview-content :deep(blockquote) {
  color: #a0aec0;
  border-left-color: #4c51bf;
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.1) 0%, rgba(85, 60, 154, 0.1) 100%);
}

.dark .preview-content :deep(blockquote::before) {
  color: rgba(76, 81, 191, 0.3);
}

.dark .preview-content :deep(li) {
  color: #cbd5e0;
}

.dark .preview-content :deep(li::marker) {
  color: #4c51bf;
}

.dark .preview-content :deep(a) {
  color: #a78bfa;
}

.dark .preview-content :deep(a:hover) {
  color: #c4b5fd;
}

.dark .preview-content :deep(a::after) {
  background: linear-gradient(90deg, #4c51bf 0%, #553c9a 100%);
}

.dark .preview-content :deep(img) {
  background-color: rgba(45, 55, 72, 0.8);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  border-color: rgba(255, 255, 255, 0.1);
}

.dark .preview-content :deep(img:hover) {
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
}

.dark .preview-content :deep(table) {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  background: rgba(45, 55, 72, 0.8);
}

.dark .preview-content :deep(th) {
  background: linear-gradient(135deg, #4c51bf 0%, #553c9a 100%);
}

.dark .preview-content :deep(td),
.dark .preview-content :deep(th) {
  border-color: rgba(255, 255, 255, 0.1);
}

.dark .preview-content :deep(tr:nth-child(even)) {
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.05) 0%, rgba(85, 60, 154, 0.05) 100%);
}

.dark .preview-content :deep(tr:hover) {
  background: linear-gradient(135deg, rgba(76, 81, 191, 0.1) 0%, rgba(85, 60, 154, 0.1) 100%);
}

.dark .preview-content :deep(hr) {
  background: linear-gradient(90deg, #4c51bf 0%, #553c9a 100%);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .preview-container {
    padding: 16px;
  }
  
  .preview-content {
    font-size: 14px;
  }
  
  .preview-content :deep(h1) {
    font-size: 1.875em;
  }
  
  .preview-content :deep(h2) {
    font-size: 1.5em;
  }
}

/* 减少动画模式支持 */
@media (prefers-reduced-motion: reduce) {
  .preview-container,
  .preview-content :deep(*) {
    animation: none;
    transition: none;
  }
}
</style>